const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const formatProductType = v => {
  //1.深股  2.上股   3.港股    4.美股   5.数字货币    6.股票基金    7.p2p    8.货币基金
  let res = '未知';
  switch (Number(v)) {
    case 1:
      res = '深股';
      break;
    case 2:
      res = '上股';
      break;
    case 3:
      res = '港股';
      break;
    case 4:
      res = '美股';
      break;
    case 5:
      res = '数字货币';
      break;
    case 6:
      res = '股票基金';
      break;
    case 7:
      res = 'p2p';
      break;
    case 8:
      res = '货币基金';
      break;
    default:
      res = '未知';
      break;
  }
  return res;
};

const baseURL = 'https://www.houseselected.com'
const http = (opt) => {
  if (getApp().globalData.userInfo) {
    if (opt.params) {
      opt.params.userId = getApp().globalData.userInfo.userId
    } else {
      opt.params = {
        userId: getApp().globalData.userInfo.userId
      }
    }
  }
  return new Promise((resovle, reject) => {
    wx.request({
      url: baseURL + opt.url,
      method: opt.type || 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: opt.params,
      success: function (res) {
        if (res.statusCode === 200) {
          resovle(res.data);
        } else {
          reject(res);
        }
      },
      fail: function (error) {
        reject(err);
      }
    })
  })
};

module.exports = {
  formatTime: formatTime,
  formatProductType: formatProductType,
  http: http
};